﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ultima.Xna.Input;

namespace Ultima.Xna.Graphics.UI.Gumps
{
    public class GumpCheckbox : GumpEntry
    {
        private int _uncheckedId;
        private int _checkedId;
        private int _switchId;

        private bool _checked;

        private Texture2D _uncheckedTexture;
        private Texture2D _checkedTexture;

        internal Texture2D UncheckedTexture
        {
            get { return _uncheckedTexture; }
            set { _uncheckedTexture = value; }
        }

        internal Texture2D CheckedTexture
        {
            get { return _checkedTexture; }
            set { _checkedTexture = value; }
        }

        public int UncheckedId
        {
            get { return _uncheckedId; }
            set { _uncheckedId = value; }
        }

        public int CheckedId
        {
            get { return _checkedId; }
            set { _checkedId = value; }
        }

        public int SwitchId
        {
            get { return _switchId; }
            set { _switchId = value; }
        }

        public virtual bool Checked
        {
            get { return _checked; }
            set { _checked = value; }
        }

        public override Vector2 Size
        {
            get
            {
                if (_checked)
                {
                    return new Vector2(_checkedTexture.Width, _checkedTexture.Height);
                }
                else
                {
                    return new Vector2(_uncheckedTexture.Width, _uncheckedTexture.Height);
                }
            }
            set
            {
                base.Size = value;
            }
        }

        public GumpCheckbox(Game game)
            : base(game)
        {
            Clickable = true;        
        }

        public override void Initialize()
        {
            base.Initialize();

            _uncheckedTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _uncheckedId);
            _checkedTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _checkedId);
        }

        protected override void OnMouseClicked(object sender, MouseEventArgs e)
        {
            base.OnMouseClicked(sender, e);

            _checked = !_checked;
        }

        public override void Draw(GameTime gameTime)
        {
            Manager.Renderer.Render(this);

            base.Draw(gameTime);
        }
    }
}
